package barbershop.com.service.orders.impl;

import barbershop.com.dao.VipDao;
import barbershop.com.dao.orders.OrderDao;
import barbershop.com.dao.orders.OrderPaymentDao;
import barbershop.com.entity.Vip;
import barbershop.com.entity.orders.OrderPayment;
import barbershop.com.entity.orders.Orders;
import barbershop.com.service.orders.OrderPaymentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@Transactional
@Service
public class OrderPaymentServiceImpl implements OrderPaymentService{
    @Autowired
    private OrderPaymentDao orderPaymentDao;
    @Autowired
    private OrderDao orderDao;
    @Autowired
    private VipDao vipDao;

    @Override
    public OrderPayment insert(OrderPayment record) {
        try {
            OrderPayment order = orderPaymentDao.save(record);
            System.out.println("支付信息添加成功！");
            return order;
        }   catch (Exception e){
            e.printStackTrace();
            System.out.println("支付信息添加失败！");
        }
        return null;
    }

    @Override
    public Boolean delete(OrderPayment record) {
        try {
            this.orderPaymentDao.delete(record);
            System.out.println("支付信息删除成功！");
            return true;
        }   catch (Exception e){
            e.printStackTrace();
            System.out.println("支付信息删除失败！");
            return false;
        }
    }

    @Override
    public OrderPayment update(OrderPayment record) {
        try {
            OrderPayment order = this.orderPaymentDao.save(record);
            System.out.println("支付信息修改成功！");
            return order;
        }   catch (Exception e){
            e.printStackTrace();
            System.out.println("支付信息修改失败！");
        }
        return null;
    }

    @Override
    public OrderPayment find(OrderPayment record) {
        try {
            Optional<OrderPayment> orderPayment = this.orderPaymentDao.findById(record.getPayId());
            if (orderPayment.isPresent()){
                System.out.println("支付信息查询成功！");
                return orderPayment.get();
            }
            return null;
        }   catch (Exception e){
            e.printStackTrace();
            System.out.println("支付信息查询失败！");
        }
        return null;
    }

    @Override
    public String updateTransAmount(Integer payId, Integer orderId) {
        try {
            Optional<Orders> order = this.orderDao.findById(orderId);
            if(order.isPresent()){
                Vip vip = order.get().getVip();
                Double totalPrice = order.get().getOrderTotalPrice();
                // 会员卡支付
                if(vip!=null){
                    System.out.println("这里是VIP："+vip.getVipName());
//                    order.get().setVip(vip);
                    Double balance = vip.getVipBalance();
                    Double discountPrice = (double)Math.round(totalPrice * 0.85);
                    // 余额充足
                    if (balance >= discountPrice){
                        this.vipDao.updateBalance(balance-discountPrice, vip.getVipId());
                        this.orderPaymentDao.updateTransAmount(discountPrice, payId);
                        this.orderDao.updateOrderStatus(1, orderId);    // 已支付
                        return "SUCCESS";
                    }
                    // 余额不足
                    else {
                        this.orderPaymentDao.updateTransAmount(0.0, payId);
                        System.out.println("会员卡余额不足！！");
                        return "LACK";
                    }
                }
                // 非会员
                else {
                    Double transAmount = order.get().getOrderTotalPrice();
                    this.orderPaymentDao.updateTransAmount(transAmount, payId);
                    this.orderDao.updateOrderStatus(1, orderId);    // 已支付
                    return "SUCCESS";
                }
            }
            System.out.println("交易支付表更新成功！");
        }   catch (Exception e){
            e.printStackTrace();
            System.out.println("交易支付表更新失败！");
        }
        return "ERROR";
    }

    @Override
    public List<OrderPayment> findAll() {
        try {
            List<OrderPayment> orderPaymentList = this.orderPaymentDao.findAll();
            System.out.println("所有交易记录获取完毕！");
            return orderPaymentList;
        }   catch (Exception e){
            e.printStackTrace();
            System.out.println("所有交易记录获取失败！");
        }
        return null;
    }

    @Override
    public Page<OrderPayment> getPage(Integer page, Integer size) {
        try {
            Pageable pageable = PageRequest.of(page-1, size);
            Page<OrderPayment> paymentsPage =this.orderPaymentDao.findAll(pageable);
            System.out.println("所有交易记录数据分页完成！");
            return paymentsPage;
        }   catch (Exception e){
            e.printStackTrace();
            System.out.println("分页失败！");
        }
        return null;
    }
//    @Override
//    public Page<OrderPayment> getPageByDate(Integer page, Integer size, String date) {
//        try {
//            Pageable pageable = PageRequest.of(page-1, size);
//            Page<OrderPayment> paymentsPage = this.findByDate(date);
//            System.out.println("所有交易记录数据分页完成！");
//            return paymentsPage;
//        }
//    }
    @Override
    public List<OrderPayment> findByDate(String date) {
        try {
            List<Orders> ordersList = this.orderDao.findOrdersByRecentDay(date);
            ArrayList<Integer> list = new ArrayList<>();
            for (Orders orders: ordersList){
                list.add(orders.getOrderId());
            }
            List<OrderPayment> orderPaymentList = new ArrayList<>();
            for (Integer id: list){
                orderPaymentList.add(this.orderPaymentDao.findByOrderId(id));
            }
            System.out.println("按日期查找的订单已找到！");
            return orderPaymentList;
        }   catch (Exception e){
            e.printStackTrace();
            System.out.println("按日期查找的订单未找到！");
        }
        return null;
    }


}
